#include "MultiLevel.h"
#include <iostream>
using namespace std;

extern int N;

MultiLevel::MultiLevel(vector<OSThread*>* threads, int quanta) :
	OSScheduler(*threads), quanta(quanta)
{
	for(int i=1; i<N; i++)
		Q[3].push(i);
}

MultiLevel::~MultiLevel()
{
}

void MultiLevel::runSomeThread(){
	int i=0;
	while(Q[i].empty()) {
		i++;
		}
	int t=Q[i].front();
	Q[i].pop();
	int used = giveQuanta(t, quanta);

	//hard working worker - higher priority
	if(used==quanta && i>0) {
		//cout << "upgrading "<<t<<endl;
		Q[i-1].push(t);
	}
	//lazy thread - lower priority
	else if(used < quanta/2 && i<3) {
		//cout << "degrading "<<t<<endl;
		Q[i+1].push(t);
	}
	else {
		//cout << "ignoring "<<t<<endl;
		Q[i].push(t); //office drone
	}

	return;
}
